package com.wdhcr.exception;

import com.wdhcr.enums.ResultCode;
import com.wdhcr.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局404/500异常捕捉处理工具
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @Autowired
    private ResponseResult responseResult;





    @ExceptionHandler
    @ResponseBody
    public ResponseResult defaultErrorHandler(HttpServletRequest request, Exception e){

        responseResult.setMessage(e.getMessage());
        if(e instanceof NoHandlerFoundException){
            responseResult.setCode(ResultCode.FAIL.getCode());
        }else {
            responseResult.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        }
        return responseResult;
    }
}
